<?php
namespace Common\Models;
use Common\BaseModel;
class BShopUsers extends BaseModel
{
    public $res;
    public $expire;
    public function initialize()
    {
        parent::initialize();
        $this->setSource('b_shop_users');
    }

    /**
     * 获取用户信息
     */
    public function get_shop_user_info($params=[]){
        $page = $params['page'] ? intval($params['page']) : 1;
        $size = $params['page_size'] ? intval($params['page_size']) : 10;
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $conditions="s.id>0";
        if(isset($params['shop_id']) && !empty($params['shop_id'])){
            $conditions .=" AND s.id={$params['shop_id']}";
        }
        $sql=<<<EFO
        SELECT s.user_id,i.shop_id,u.phone,u.nickname,u.avatar,c.category_name,u.nickname,s.created_at,i.shop_name,i.shop_avatar,s.status,i.shop_real_name,i.id_card,category_id,shop_address,latitude_longitude,shop_img,business_license,bond_price,is_bond,disable_tips,id_card_img,shop_introduction
        FROM b_shop_users as s
        LEFT JOIN f_users as u on u.id=s.user_id
        LEFT JOIN b_shop_users_info as i on i.shop_id=s.id
        LEFT JOIN b_shop_goods_category as c on c.id=i.category_id
        WHERE $conditions ORDER BY s.created_at desc LIMIT $page,$size 
EFO;
        $get_list['items'] = $this->getDI()->get('dbMaster')->fetchAll($sql);
        foreach($get_list['items'] as $key=>$value){
            $get_list['items'][$key]['id_card_img'] = $value['id_card_img'] ? explode(',',$value['id_card_img']) : [];
        }
        $get_list['total'] = $this->count(['','column'=>'id']);
        return $get_list;
    }


    /**
     * 通过店铺id获取店铺的user_id
     */
    public function find_shop_to_user_id($shop_id){
        $shop_user = $this->findFirstById($shop_id);
        if(!$shop_user){
            return 0;
        }
        return $shop_user->user_id;
    }


    /**
     * 获取用户的权限组
     */
    public function shopToRole($params){
        $page = $params['page'] ? intval($params['page']) : 1;
        $size = $params['page_size'] ? intval($params['page_size']) : 10;
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $conditions="u.id>0";
        $sql=<<<EFO
        SELECT u.*,r.name as role_name FROM b_shop_users AS u
        LEFT JOIN b_shop_roles as r on u.role_group = o.id
        WHERE $conditions ORDER BY u.created_at desc LIMIT $page,$size 
EFO;
        $items = $this->getDI()->get('dbMaster')->fetchAll($sql);
        $get_list['items'] = $items;

        $sql_count=<<<EFO
        SELECT count(u.id) as count FROM b_shop_users AS u
        LEFT JOIN b_shop_roles as r on u.role_group = o.id
        WHERE $conditions ORDER BY u.created_at desc LIMIT 1 
EFO;
        $count = $this->getDI()->get('dbMaster')->fetchOne($sql_count);
        $get_list['total'] = isset($count['count']) ? (int)$count['count'] : 0;
        return $get_list;
    }


}
